<?php

class Users_Model_Access_Resource extends Workset_Model_Object_Abstract {

	const HAS_PRIVILEGES = 1;
	const HAS_NOT_PRIVILEGES = 0;

	protected $_hasRelations = true;

    protected $_privileges = array();

    protected $_privilegesToDelete = array();

    function addPrivilege(Users_Model_Access_Privilege $privilege) {
        $this->_privileges[] = $privilege;
		return $this;
    }

    function deletePrivilege(Users_Model_Access_Privilege $privilege) {
        $this->_privilegesToDelete[] = $privilege;
		return $this;
    }

    function save() {
        $result = parent::save();

        if ($result) {
            if ($this->_privilegesToDelete) {
                foreach ($this->_privilegesToDelete as $p) {
                    $this->unlinkObject($p);
                }
            }

            if ($this->_privileges) {
                foreach ($this->_privileges as $p) {
                    $this->linkToObject($p);
                }
            }
        }

        //очистка очереди
        $this->_clean();

        return $result;
    }

    protected function _clean() {
        $this->_privileges = array();
        $this->_privilegesToDelete = array();
    }

}

?>